home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 19 / madtrb11.zip / INTERUPT.PAS < prev    next >
Pascal/Delphi Source File  |  1985-07-13  |  5KB  |  159 lines

  1. {
  2.      T U R B O    P a s c a l    I n t e r r u p t    H a n d l e r
  3.  
  4. This program demonstrates the use of interrupts on the IBM PC from
  5. programs written in Turbo Pascal.
  6.  
  7. Source: "Writing Interrupt Procedures in Turbo Pascal", TUG Lines Volume I Issue 6
  8. Author: Jim Camelford
  9. Application: IBM PC and true compatibles
  10.  
  11. Copyright (c) Jim Camelford and CompuCam - All Rights Reserved
  12.  
  13. **********************************************************************
  14. }
  15.  
  16. {$V-}      {Never ... never change these}
  17. {$K-}
  18. {$C-}
  19. Type
  20.     address =     ^integer;
  21.  
  22. Var
  23.     vector:       array [0..$1f] of address  absolute $0000:$0000;
  24.     turboDTA:     address;
  25.  
  26.  
  27. Const
  28.     csdseg:       integer = 0;
  29.     csstacksave:  integer = 0;
  30.     csspsave:     integer = 0;
  31.     csturboseg:   integer = 0;
  32.     csturbosp:    integer = 0;
  33.     cssf:         integer = -1;    {normally -1}
  34.     csmemspace:   integer = 0;
  35.  
  36. {**********************************************************************
  37.               S e t    T u r b o    E n v i r o n m e n t
  38. **********************************************************************}
  39.  
  40. Procedure SetTurboEnvironment;
  41.   begin  {setturboenvironment}
  42.  
  43.   {------------------------------------------------------------
  44.   This procedure must never be altered such that it has any local
  45.     variables or a parameter.  After entry, its stack must look
  46.     exactly like an interrupt procedures stack
  47.   ------------------------------------------------------------}
  48.  
  49.   csdseg := dseg;
  50.   csturboseg := sseg;
  51.   inline($2e/$89/$26/csturbosp);
  52.   csmemspace := (sseg-cseg)*16+csturbosp+6+16;
  53.   end;  {of setturboenvironment}
  54.  
  55. Procedure Handler;
  56.   Forward;
  57.  
  58. {**********************************************************************
  59.               I n t e r r u p t    P r o c e d u r e
  60. **********************************************************************}
  61.  
  62. Procedure Interrupt;
  63.   begin  {interrupt}
  64.   inline($2e/$ff/$06/cssf/           {   inc cs:cssf       }
  65.          $75/$14/                    {   jne *+10h      }
  66.          $2e/$8c/$16/csstacksave/    {   saves current stack segment }
  67.          $2e/$89/$26/csspsave/       {   saves current stack ptr }
  68.          $2e/$8e/$16/csturboseg/     {   points stack to our stack segment}
  69.          $2e/$8b/$26/csturbosp);     {   points sp    to out stack segment}
  70.  
  71.   inline($50/$53/$51/$52/$57/$56/$06/$1e);  { Push ax, bx, cx, dx,
  72.                                                    di, si, es, ds}
  73.  
  74.   inline($2e/$a1/csdseg/         {   mov ax,cs:csdseg }
  75.          $8e/$d8/                {   mov ds,ax   }
  76.          $fb);                   {   sti  }
  77.  
  78.   Handler;
  79.  
  80.   inline($fa/                                { interrupts off }
  81.          $1f/$07/$5e/$5f/$5a/$59/$5b/$58/    { Pop  ds, es, si, di,
  82.                                                     dx, cx, bx, ax}
  83.          $2e/$ff/$0e/cssf/                   { dec  cs:cssf }
  84.          $7d/$0d/                            { jge  *+13 }
  85.          $2e/$8e/$16/csstacksave/            { mov  ss,cs:csstacksave }
  86.          $2e/$8b/$26/csspsave/               { mov  sp,cs:csspsave    }
  87.          $5d/$5d/$cf/                        { pop 2 temporaries and iret}
  88.  
  89. {+0d:}   $8b/$e5/                            { mov sp,bp }
  90.          $5d/                                { pop bp    }
  91.          $cf);                               { iret      }
  92.  
  93.   end;  {of interrupt}
  94.  
  95. Var
  96.   TickVec:                 address;    {vector's original contents}
  97.   tix, hours, mins, secs:  integer;    {timer registers}
  98.  
  99. Const
  100.   TimerAddr = $1c;                     { $1c = Get Control on Timer}
  101.  
  102. {*************************************************************************
  103.                      H a n d l e r    P r o c e d u r e
  104. *************************************************************************}
  105.  
  106. Procedure Handler;
  107. Var
  108.   x,y:  byte;
  109.  
  110.   begin
  111.   {This procedure is entered 18 times / second.  It bumps the timer
  112.     registers and displays the contents once each second in the upper
  113.     right corner of the screen}
  114.  
  115.   tix := tix+1;
  116.   if tix >= 18 then
  117.     begin
  118.     tix := 0;
  119.     secs := secs+1;
  120.     if secs=60 then
  121.       begin
  122.       secs := 0;
  123.       mins := mins+1;
  124.       if mins=60 then
  125.         begin
  126.         mins := 0;
  127.         hours := hours+1;
  128.         if hours=24 then hours := 0;
  129.         end;
  130.       end;
  131.     x := wherex; y := wherey;
  132.     gotoxy(60,1);
  133.     write(hours:2, ':', mins:2, ':', secs:2);
  134.     gotoxy(x,y);
  135.     end;
  136.  
  137.   end;  {of handler}
  138.  
  139. {*********************************************************************
  140.                   M a i n    P r o g r a m
  141. *********************************************************************}
  142.  
  143. begin
  144. cssf := 0;             {Must set this to 0 to indicate that we are
  145.                         not running as an extension to DOS ... that is
  146.                         we are already in Turbo's execution space}
  147.  
  148. SetTurboEnvironment;
  149.  
  150. tix := 0;  hours := 0;  mins:=0;  secs:=0;        {initialize timer}
  151.  
  152. TickVec := vector[TimerAddr];                     {attach ourselves to $1c}
  153. vector[TimerAddr] := ptr(cseg, ofs(Interrupt));
  154. repeat until keypressed;
  155.  
  156. vector[TimerAddr] := TickVec;                     {restore original contents}
  157.                                                   {... before leaving}
  158. end.
  159.